package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CompanyFroumMyAnswerAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CompanyForumMyAnswer;
import com.isunland.managesystem.entity.CompanyForumTopic;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 对象功能:  我的回复列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/21 16:07
 */
public class AgriculturalForumMyReplyListFragment extends BaseListFragment {
  public static final String EXTRA_JOBNO =
      "com.isunland.managesystem.ui.AgriculturalForumMyReplyListFragment.EXTRA_JOBNO";

  private String mJobno;
  private CompanyFroumMyAnswerAdapter mAdapter;
  private String mMainId;
  private BaseVolleyActivity mActivity;

  public static AgriculturalForumMyReplyListFragment newInstance(String jobno) {

    Bundle args = new Bundle();
    args.putString(EXTRA_JOBNO, jobno);
    AgriculturalForumMyReplyListFragment fragment = new AgriculturalForumMyReplyListFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    setBack(true);
    mActivity = (BaseVolleyActivity) getActivity();
    mJobno = getArguments().getString(EXTRA_JOBNO);
  }

  @Override public String getUrl() {
    return ApiConst.URL_PERSON_REPLY_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("jobNo", mJobno);
    /** 01: 农技服务,02: 农机服务*/
    params.put("topicKindCode",
        SharedPreferencesUtil.getString(mActivity, SharedPreferencesUtil.KEY_FORUM_SUBCODE, ""));
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    CompanyForumMyAnswer myAnswer = new Gson().fromJson(arg0, CompanyForumMyAnswer.class);
    List<CompanyForumMyAnswer.MyAnswerDetail> answerDetail = myAnswer.getRows();
    setAdapter(answerDetail);
  }

  private void setAdapter(List<CompanyForumMyAnswer.MyAnswerDetail> answerDetail) {
    if (answerDetail == null) {
      answerDetail = new ArrayList<>();
    }

    if (mAdapter == null) {
      mAdapter = new CompanyFroumMyAnswerAdapter(getActivity(), answerDetail);
      setListAdapter(mAdapter);
      return;
    }
    ((CompanyFroumMyAnswerAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    CompanyForumMyAnswer.MyAnswerDetail answerDetail = mAdapter.getItem(position - 1);
    mMainId = answerDetail.getMainId();
    getCommentTopic();// 获取评论对应的话题
    super.onListItemClick(l, v, position, id);
  }

  private void getCommentTopic() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_KNOWLEDGEFORUM_POST_LIST);
    HashMap<String, String> params = new HashMap<>();
    params.put("id", mMainId);
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) {
        CompanyForumTopic commentTopic = new Gson().fromJson(arg0, CompanyForumTopic.class);
        String omentTopic = commentTopic.getTotal();
        List<CompanyForumTopic.actualObject> topicDetail = commentTopic.getData();
        // 只有一个话题。
        if ("0".equals(omentTopic)) {
          ToastUtil.show(R.string.notExistTopic);
        } else {
          CompanyForumTopic.actualObject actualObject = topicDetail.get(0);
          Intent intent = new Intent(getActivity(), AgriculturalForumDetailAcrivity.class);
          intent.putExtra(AgriculturalForumDetailFragment.EXTRA_VALUE, actualObject);
          startActivity(intent);
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        LogUtil.e("VolleyError==" + arg0);
        // 错误提示。
        ToastUtil.show(R.string.dataAnyError);
      }
    });
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  /**
   * 响应返回按钮
   */
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case android.R.id.home:
        getActivity().finish();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }
}
